package com.ykt.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.interceptor.Seach;

public class Order extends SeachModel<Order>{

	private static final long serialVersionUID = 1L;
	public static final Order dao = new Order();
	@Override
	public Page<Order> paginate(int pageNumber, int rows, Seach seach) {
		String select="select a.*,s.name as shop_name,c.username as username";
		String sqlExceptSelect = " from orders a left join shop s on a.shop_id = s.id "
				+ " left join customer c on c.id = a.customer_id where 1=1 ";
		return paginateBySelect(select, sqlExceptSelect, pageNumber, rows, seach);
	}
	//得到商户
	public Shop getShop(){
		return Shop.dao.findById(getInt("shop_id"));
	}
	public Customer getCustomer(){
		return Customer.dao.findById(getInt("customer_id"));
	}
	/**
	 * 一对多 查询出订单下面 的所有订单详细
	 * @return
	 */
	public List<OrderDetail> getOrderDetails(){
		Integer id =getInt("id");
		if(id!=null&&id>0) return OrderDetail.dao.find("select a.*,p.*  from order_detail a left JOIN product p on a.product_id = p.id  where a.order_id =? ",id);
		else return null;
			
	}
	public String getPostAddress(){
		Integer addId = getInt("customer_address_id");
		if(addId==null){
			return null;
		}
		CustomerAddress add = CustomerAddress.dao.findById(get("customer_address_id"));
		return add.getStr("prov")+"省"+add.getStr("city")+add.getStr("area")+"  "+add.getStr("address");
	}
	public CustomerAddress getAddr(){
		Integer addId = getInt("customer_address_id");
		if(addId==null){
			return null;
		}
		return CustomerAddress.dao.findById(get("customer_address_id"));
	}
	
}
